<template>
  <div class="container">
    <div class="card_container">
      <div class="product_card  animate__animated animate__backInLeft" v-for="item in dataSource" :key="item.goods_id">
        <div class="img_bg">
          <a href="#" @click.prevent="toProductDetails(item)">
            <img :src="item.goods_img" alt=""
          /></a>
        </div>
        <div class="details">
          <p>&nbsp;&nbsp;{{ item.goods_title }}</p>
          <p>￥{{ item.goods_price }}元</p>
          <p @click="toProductDetails(item)">
            <a href="#">立即购买&nbsp;&gt;</a>
          </p>
        </div>
      </div>
    </div>
    <el-empty
      v-show="dataSource.length == 0"
      description="暂无符合的商品"
    ></el-empty>
  </div>
</template>
<script>
import $ from "jquery"
setTimeout($('#myDiv').empty, 500);
import pubsub from "pubsub-js";
export default {
  name: "IssueShowCard",
  data() {
    return {
      star: true,
      product: [],
      lowPrice: "",
      highPrice: "",
      fromPage: "",
    };
  },
  methods: {
    toProductDetails(item) {
      this.$router.push({
        path: "/productdetails",
        query: { id: item.goods_id },
      });
    },
  },

  async mounted() {
     await this.$bus.$on("fromName", (res) => {
      this.fromPage = res;
    });
    if (this.fromPage == 'Home' || this.fromPage == 'ProductManIssue' || this.fromPage == 'ProductWomanIssue') {
      this.product = this.$route.params.id;
    } 
      pubsub.unsubscribe("productPrice", "productobj");
      pubsub.subscribe("productPrice", (_, res) => {
        this.lowPrice = res.p1;
        this.highPrice = res.p2;
      });
      pubsub.subscribe("productobj", (_, res) => {
        if (res == undefined) {
          this.product = [];
        } else {
          this.product = res;
        }
      });
    
  },
  computed: {
    dataSource() {
      return this.product.filter((e) => {
        if (this.lowPrice == "" && this.highPrice == "") {
          return e;
        } else if (this.lowPrice == "" && this.highPrice != "") {
          this.lowPrice = 0;
          if (
            parseInt(e.goods_price) >= parseInt(this.lowPrice) &&
            parseInt(e.goods_price) <= parseInt(this.highPrice)
          ) {
            return e;
          }
        } else {
          if (
            parseInt(e.goods_price) >= parseInt(this.lowPrice) &&
            parseInt(e.goods_price) <= parseInt(this.highPrice)
          ) {
            return e;
          }
        }
      });
    },
  },
};
</script>
<style>
.details > p:nth-child(1) {
  font-size: 15px;
  height: 40px;
  line-height: 20px;
  font-weight: 200;
  text-align: center;
  overflow: hidden;
}
.details > p:nth-child(2),
.details > p:nth-child(3) {
  font-size: 12px;
  line-height: 15px;
  font-weight: 400;
  text-align: center;
}
.details > p:nth-child(2) {
  color: #ccc;
}
.details > p:nth-child(3) a {
  text-decoration: none;
  color: #000;
}

.img_bg {
  width: 100%;
  height: 80%;
  position: relative;
}
i {
  position: absolute;
  top: 10px;
  right: 20px;
  font-size: 20px;
}
.img_bg img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.card_container {
  display: flex;
  flex-wrap: wrap;
}
.card_container > div {
  margin-left: 40px;
}
.product_card {
  width: 20%;
  height: 400px;
  margin: 15px;
}
</style>